﻿// ***********************************************************************
// Assembly         : STX.Lib
// Author           : seven.x
// Created          : 12-27-2013
//
// Last Modified By : seven.x
// Last Modified On : 12-27-2013
// ***********************************************************************
// <copyright file="Common.cs" company="驰创数码">
//     Copyright (c) 驰创数码. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************

namespace STX.Lib.Utils
{
    /// <summary>
    /// Class Common.
    /// </summary>
    public class Common
    {
        /// <summary>
        /// The environment
        /// </summary>
        public static EnvironmentType Environment = EnvironmentType.WEB;
        /// <summary>
        /// The root path
        /// </summary>
        public static string RootPath = "/";
        /// <summary>
        /// Maps the path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>System.String.</returns>
        public static string MapPath(string path)
        {
            if (Environment == EnvironmentType.WEB)
            {
                
                return System.Web.HttpContext.Current.Server.MapPath(path);
            }
            return RootPath + path;
        }

        /// <summary>
        /// Initializes static members of the <see cref="Common"/> class.
        /// </summary>
        static Common()
        {
            var p = System.Diagnostics.Process.GetCurrentProcess();
            if (p.ProcessName == "w3wp" || p.ProcessName.StartsWith("WebDev.WebServer"))
            {
                Environment = EnvironmentType.WEB;
                RootPath = System.Web.HttpContext.Current.Server.MapPath("~/");
            }
            else
            {
                Environment = EnvironmentType.DESKTOP;
                RootPath = System.Environment.CurrentDirectory;
            }
        }
    }


    /// <summary>
    /// Enum EnvironmentType
    /// </summary>
    public enum EnvironmentType
    {
        /// <summary>
        /// The web
        /// </summary>
        WEB = 0,
        /// <summary>
        /// The desktop
        /// </summary>
        DESKTOP = 1
    }
}
